package hu;


import java.util.*;

public class hu68_2 {
    static class people{
        String name;
        int score;

        public people(String name, int score) {
            this.name = name;
            this.score = score;
        }

    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        int order = sc.nextInt();
        sc.nextLine();
        List<people> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String[] s = sc.nextLine().split(" ");
            people p = new people(s[0], Integer.parseInt(s[1]));
            list.add(p);
        }
        if(order == 1)
            Collections.sort(list, (a, b) ->{
                return a.score - b.score;
            });
        else{
            Collections.sort(list, (a, b) ->{
                return b.score - a.score;
            });
        }
        for (int i = 0; i < n; i++) {
            people p = list.get(i);
            System.out.println(p.name + " " + p.score);
        }

    }
}
/*
3
0
fang 90
yang 50
ning 50
 */